<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.keyword" placeholder="输入手机号或昵称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="地区:" prop="areaId">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="代理类型:">
          <el-select v-model="listPram.roleId" placeholder="请选择" style="width: 200px;">
            <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.data"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否为商家:">-->
        <!--          <el-select v-model="listPram.isBusiness" placeholder="请选择" style="width: 200px;">-->
        <!--            <el-option-->
        <!--                v-for="item in isBusinessList"-->
        <!--                :key="item.id"-->
        <!--                :label="item.data"-->
        <!--                :value="item.id">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="身份:">
          <el-select v-model="listPram.identity" placeholder="请选择" style="width: 200px;">
            <el-option
                v-for="item in identityList"
                :key="item.id"
                :label="item.data"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-left:100px;">
          <span>用户总数 {{ listData.count }}</span>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="ID" prop="id" width="120"></el-table-column>
        <el-table-column label="头像" prop="avatar">
          <template slot-scope="scope">
            <el-image :src="imageUrl + scope.row.faceUrl"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="昵称" prop="name">
          <template slot-scope="scope">
            <div> {{ scope.row.name || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="phone"/>
        <el-table-column label="所在地区" prop="areaText"/>
        <el-table-column label="最后登录时间" prop="lastLoginTime"/>
        <el-table-column label="益联券" prop="voucher"/>
        <el-table-column label="抵用金" prop="deductionAmount"/>
        <el-table-column label="积分" prop="integral"/>
        <el-table-column label="付款码" prop="avatar">
          <template slot-scope="scope">
            <el-image :src="imageUrl + scope.row.beautifyAccountsCodeUrl" :preview-src-list="preList"
            @click="preview(imageUrl + scope.row.beautifyAccountsCodeUrl)"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="邀请码" prop="avatar">
          <template slot-scope="scope">
            <el-image :src="imageUrl + scope.row.beautifyInvitationCodeUrl"
                      :preview-src-list="preList"
                      @click="preview(imageUrl + scope.row.beautifyInvitationCodeUrl)"
            ></el-image>
          </template>
        </el-table-column>
        <!--        <el-table-column label="月消费益联券" prop="allCoupons"/>-->
        <!--        <el-table-column label="月消费积分" prop="inviteOrderPoints"/>-->
        <!--        <el-table-column label="是否会员" min-width="50" prop="vip">-->
        <!--          <template slot-scope="scope">-->
        <!--            <div style="color:#15f50d;" v-if="scope.row.vip===1">是</div>-->
        <!--            <div style="color:#1224ee;" v-if="scope.row.vip===2">否</div>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="是否商家" min-width="50" prop="status">-->
        <!--          <template slot-scope="scope">-->
        <!--            <div style="color:#1224ee;" v-if="scope.row.status===1">否</div>-->
        <!--            <div style="color:#15f50d;" v-if="scope.row.status===2">是</div>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" min-width="300" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-has-permi="['admin:user:userGrant']" icon="el-icon-document-remove" size="mini"
                       type="success"
                       @click="openUserGrant(scope.row.id,scope.row)">发放
            </el-button>
            <el-button icon="el-icon-s-order" size="mini" type="primary" @click="openUserEdit(scope.row.id)"
                       v-has-permi="['admin:user:detail']">用户详情
            </el-button>
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEdit(scope.row.id)"
                       v-has-permi="['admin:user:detail']">记录详情
            </el-button>
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openInvitationEdit(scope.row.id)"
                       v-has-permi="['admin:user:detail']">邀请详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="editDialog.id? '记录详情':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="editUserDialog.show"
        :title="editUserDialog.id? '用户详情':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <userEdit
          v-if="editUserDialog.show"
          :id="editUserDialog.id"
          @hideUserEditDialog="hideUserEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        title="发放益联券"
        :visible.sync="userGrant.show"
        destroy-on-close
        top="80px"
        width="1200px">
      <UserGrant
          v-if="userGrant.show"
          :id="userGrant.id"
          :edit-data="userGrant.editDate"
          ref="editForm"
          @hideGrantDialog="hideUserGrantDialog"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="editInvitationDialog.show"
        title="用户邀请详情"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px">
      <invitationEdit
          v-if="editInvitationDialog.show"
          :id="editInvitationDialog.id"
          @hideEditDialog="hideInvitationEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import userEdit from './userEdit'
import {deleteRow, list} from "@/api/user";
import {checkPermi} from "@/utils/permission";
import UserGrant from "@/views/user/member/userGrant.vue";
import {optionsList, userNews} from "@/api/business";
import InvitationEdit from "@/views/user/member/invitationEdit.vue";

export default {
  components: {InvitationEdit, UserGrant, edit, userEdit},
  data() {
    return {
      imageUrl: '',
      preList:[],
      listData: {list: [], count: 0},
      listPram: {
        keyword: '',
        areaId: '',
        // ifVip: 1,
        // roleId: null,
        // isBusiness: null,
        page: 1,
        limit: 20,
        identity: ''
      },
      editDialog: {
        show: false,
        id: null
      },
      editUserDialog: {
        show: false,
        id: null
      },
      editInvitationDialog: {
        show: false,
        id: null
      },
      userGrant: {
        show: false,
        id: null,
        editDate: {},
        pid: null
      },
      tableLoading: false,
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
      dataStatistics: '',
      statusList: [{id: 3, data: '市代理'}, {id: 4, data: '区代理'}],
      isBusinessList: [{id: 2, data: '是'}, {id: 1, data: '否'}],
      identityList: [{id: 1, data: '商家'}, {id: 2, data: '会员'}, {id: 3, data: '区代'}, {id: 4, data: '市代'}, {
        id: 5,
        data: '普通会员'
      }]
    }
  },
  mounted() {

    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
    this.getOptions()
    this.getDataStatistics()
  },
  created() {
    if (this.$route.query.keyword) {

      this.listPram.keyword = this.$route.query.keyword
    }
  },
  methods: {
    preview(url)
    {
      this.preList=[];
      this.preList.push(url)
    },
    getDataStatistics() {
      userNews(1).then(res => {
        this.dataStatistics = res.data
      })
    },
    handleChange(value) {
      this.listPram.areaId = value[2]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData.list = res.list;
        this.listData.count = res.count;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openUserEdit(id = null) {
      this.editUserDialog.id = id;
      this.editUserDialog.show = true
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },
    openInvitationEdit(id = null) {
      this.editInvitationDialog.id = id;
      this.editInvitationDialog.show = true
    },
    openUserGrant(id = null, editDate) {
      this.userGrant.id = id;
      this.userGrant.show = true
      this.userGrant.editDate = editDate
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideUserEditDialog() {
      this.editUserDialog.show = false
      this.getList()
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    hideInvitationEditDialog() {
      this.editInvitationDialog.show = false
      this.getList()
    },
    hideUserGrantDialog() {
      this.userGrant.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    // handleStatusChange(status,id){
    // const loading=this.$loading({text:'更新中',background:'rgba(255,255,255,.5)'})
    // updateStatus(id,status).then(()=>{
    //   this.$message.success('更新状态成功')
    //   this.getList()
    // }).finally(()=>{
    //   loading.close()
    // })
    // },
    resetQuery() {
      this.listPram.keyword = '';
      this.listPram.areaId = '';
      this.listPram.roleId = '';
      this.listPram.isBusiness = '';
      this.listPram.identity = '';
      this.listPram.page=1
      this.value = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
